package org.hughie.sync;

import lombok.extern.slf4j.Slf4j;
import org.hughie.api.FundApisMapping;
import org.hughie.common.FundSyncType;
import org.hughie.http.IHttpInvoker;
import org.hughie.mapper.HisNavMapper;
import org.hughie.model.fmn.NavFMN;
import org.hughie.model.po.BatchJob;
import org.hughie.util.TimeUtil;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.ExecutorService;

/**
 * @author hughie.cheng
 * @date 2024/3/12
 */
@Slf4j
@Component
public class FundHistoryNavTask implements FundSyncTask {

    private IHttpInvoker invoker;
    private HisNavMapper mapper;
    @Qualifier("syncThreadExecutor")
    private ExecutorService executor;

    public FundHistoryNavTask(IHttpInvoker invoker, HisNavMapper mapper, ExecutorService executor) {
        this.invoker = invoker;
        this.mapper = mapper;
        this.executor = executor;
    }

    @Override
    public String getTaskCode() {
        return FundSyncType.HIS_NAV.getTaskName();
    }

    @Override
    public void sync(String fundCode) {
        List<NavFMN> list = invoker.getList(FundApisMapping.FUND_HISTORY_NAV, fundCode, "ln", TimeUtil.getCurrentMills());
        log.info("接口查询历史净值信息 基金代码：{}, {}条记录", fundCode,list.size());
        executor.submit(() -> mapper.save(fundCode, list));
    }

    @Override
    public void syncBatch(List<BatchJob> jobs) {
        jobs.forEach(t -> sync(t.getFundCode()));
    }
}
